import { Body, Inject, Post, Get, Query } from '@midwayjs/core';
import {
    CoolController, BaseController, TagTypes,
    CoolTag
} from '@cool-midway/core';

import { OrderOrderEntity } from '../../../order/entity/order';
import { AppOrderPayService } from '../../service/orderPay';

import { UserInfoEntity } from '../../../user/entity/info';
import { NursingPersonEntity } from '../../../nursing/entity/person'
import { AddressInfoEntity } from '../../../address/entity/info'

/**
 * 订单信息
 */
@CoolController({
    api: ['add', 'delete', 'update', 'info', 'list', 'page'],
    entity: OrderOrderEntity,
    service: AppOrderPayService,
    pageQueryOp: {
        fieldEq: ['a.orderNum', 'a.userId'],
        keyWordLikeFields: ['a.consignee', 'a.phoneNumber'],
        fieldLike: [],
        // fieldEq: [
        //   { column: 'a.status', requestParam: 'status' },
        //   { column: 'a.payType', requestParam: 'payType' },
        // ],
        where: ctx => {
            const { createTime } = ctx.request.body;
            if (createTime && createTime.length === 2) {
                return [
                    [
                        'a.createTime BETWEEN :start AND :end',
                        { start: createTime[0], end: createTime[1] },
                    ],
                ];
            }
        },
        select: ['a.*', 'b.name as beNurseName', 'c.province', 'c.city', 'c.district', 'c.detailAddress', 'd.nickName'],
        join: [
            {
                entity: NursingPersonEntity,
                alias: 'b',
                condition: 'a.beNurseId = b.id',
                type: 'leftJoin',
            },
            {
                entity: AddressInfoEntity,
                alias: 'c',
                condition: 'a.beNurseAddressId = c.id',
                type: 'leftJoin',
            },
            {
                entity: UserInfoEntity,
                alias: 'd',
                condition: 'a.openid = d.unionid',
                type: 'leftJoin',
            }
        ],
    },
})
export class AppOrderController extends BaseController {
    @Inject()
    appOrderPayService: AppOrderPayService;

    @Inject()
    ctx;

    // 微信支付
    @CoolTag(TagTypes.IGNORE_TOKEN)
    @Post("/createOrder", { summary: "生成订单" })
    async createOrder(@Body() BodyData: any) {
        let res = await this.appOrderPayService.createOrder(BodyData)
        return res;
    }

    // 获取订单分页
    @Get("/getOrderPage", { summary: "获取订单分页" })
    async getOrderPage(@Query() BodyData: any) {
        let res = await this.appOrderPayService.getOrderPage(BodyData)
        return res;
    }

    // 获取订单详情
    @Get("/getOrderInfo", { summary: "获取订单详情" })
    async getOrderInfo(@Query() BodyData: any) {
        let res = await this.appOrderPayService.getOrderInfo(BodyData)
        return res;
    }

    // 微信支付回调
    @CoolTag(TagTypes.IGNORE_TOKEN)
    @Post("/wxOrderResult", { summary: "获取订单状态" })
    async wxOrderResult(@Body() BodyData: any) {
        console.log(BodyData, 'query1121')
        return this.ok(await this.appOrderPayService.wxOrderResult(BodyData));
    }
}
